package job.manager.client.mb;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import job.manager.db.ec.JobEB;
import job.manager.db.ec.ProfilesEB;
import job.manager.db.ec.UsersEB;
import job.manager.db.sb.JobRemote;
import job.manager.db.sb.ProfileRemote;
import job.manager.db.sb.UserRemote;
import org.springframework.security.context.SecurityContextHolder;

/**
 *  The job ManagedBean is responsible for connection to the Enterprise Beans
 *  and providing the pages/user with the creation of jobs.
 *
 */

public class JobMB {
    @EJB
    private UserRemote userBean;
    @EJB
    private ProfileRemote profileBean;
    @EJB
    private JobRemote jobBean;

    private int id;
    private String name;
    private String comment;
    private int numberOfPeople;
    private int customerId;
    private JobEB job;
    private ProfilesEB customer;
    private String errorMsg;
    

    /** Creates a new instance of JobMB */
    public JobMB() 
    {
        onInit();
    }

    @PostConstruct
    public void onInit() {
        this.id = -1;
        this.name = "";
        this.comment = "";
        this.numberOfPeople = 0;
        this.customerId = -1;
        this.job = null;
    }

    //load a job
    public void loadJob() {
        problemSolver();
        if(this.id > 0 ) {
            this.job = jobBean.getJob(this.id);
        }
    }

    protected void checkIfNotAdmin() {
        this.problemSolver();
        String userNamn = SecurityContextHolder.getContext().getAuthentication().getName();
        UsersEB currentUser = userBean.findUser(userNamn);
        if(!currentUser.getIsAdmin()) {
            this.customerId = currentUser.getProfileId();
        }
    }
    
    //Set the values to this class from the entity
    public void setValuesToBean(){
        if(this.job != null){
            this.name = job.getName();
            this.comment = job.getComment();
            this.numberOfPeople = job.getNumberOfPeople();
            this.customer = job.getCustomerId();
        }
    }

    //Set the values to the entity class before creating
    public void setValuesToEntity(){
        if(this.job != null){
            job.setName(this.name);
            job.setComment(this.comment);
            job.setNumberOfPeople(this.numberOfPeople);
            job.setCustomerId(profileBean.getProfileInfo(this.customerId));
        }
    }

    //create job
    public void createJob(){
        this.problemSolver();
        if(this.id == -1 ) {
           this.job = new JobEB();
           this.setValuesToEntity();
           if(true)
               this.errorMsg = "Misslyckades med skapande av jobb.";
           else
               this.errorMsg = "Uppdraget " + this.name + " skapat";
        }
    }

    //set and get methods

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public int getNumberOfPeople() {
        return numberOfPeople;
    }

    public void setNumberOfPeople(int numberOfPeople) {
        this.numberOfPeople = numberOfPeople;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    //Our bug fix for spring secirity, if nullpointers get refrence from ConnectFactory
    private void problemSolver() {
        if (this.jobBean == null) {
            this.jobBean = ConnectFactory.getJobBean();
        }
        if(this.userBean == null)
            this.userBean = ConnectFactory.getUserBean();
    }

}
